/*
 * @Descripttion:
 * @version:
 * @Author: yuanliutech
 * @Date: 2022-12-05 19:03:01
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2025-03-24 21:30:47
 */
/**
 * @param {string} mobile 验证
 * @returns {Boolean}
 * @lwj
 */
export const validMobile = (rule, value, callback) => {
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (!reg.test(value)) return callback(new Error("请输入正确的手机号"));
    callback();
};

/**
 * @name: 多个imei输入校验
 * @msg: 
 * @return {*}
 */
export const validImeis = (rule, value, callback) => {
    if (value == '') return; //可以为空
    const regex = /^(\d{15},)*\d{15}(,)?$/;
    if (!regex.test(value)) return callback(new Error("请输入正确格式的IMEI号，IMEI为15位数字"))
    callback()
}

export const validMobile3 = (rule, value, callback) => {
    if (value == "") return callback();
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (!reg.test(value)) return callback(new Error("请输入正确的手机号"));
    callback();
};

/**
 * @param {string} password 验证
 * @returns {Boolean}
 * @lwj
 */
export const validatePass = (rule, value, callback) => {
    const reg = /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,16}$/;
    if (!reg.test(value))
        return callback(new Error("密码必须是6-16位的数字、字母或字符组成"));
    callback();
};

/**
 * @param {string} verification 图片验证码
 * @returns {Boolean}
 * @lwj
 */
export const validateCode = (rule, value, callback) => {
    const reg = /^[a-zA-Z0-9]{4}$/;
    if (!reg.test(value)) return callback(new Error("请输入正确的验证码"));
    callback();
};

/**
 * @param {string} validateSmsCode 手机短信验证码
 * @returns {Boolean}
 * @lwj
 */
export const validateSmsCode = (rule, value, callback) => {
    const reg = /^\d{6}$/;
    if (!reg.test(value)) return callback(new Error("请输入正确的验证码"));
    callback();
};

/**
 * @param {string} 设备编码
 * @returns {Boolean}
 * @lwj
 */
export const validateImei = (rule, value, callback) => {
    // const reg = /^\d{15}$/;
    // if (!reg.test(value)) return callback(new Error("IMEi为15位数字"))
    if (value.length < 3) return callback(new Error("设备编码最少为3位"));
    callback();
};

/**
 * @param {string} validateSmsCode 标识符
 * @returns {Boolean}
 * @lwj
 */
export const validateidentifier = (rule, value, callback) => {
    const reg = /^[a-zA-Z].*/;
    if (!reg.test(value)) return callback(new Error("请输入合法的标识符"));
    callback();
};

/**
 * @name: 普通表单限制
 * @msg: 1、空格  2、特殊字符&  3、表情符号
 * @return {*}
 */
export const validSpaceSpecialCharEmoji = (rule, str, callback) => {
    // 空格
    const reg = /^[^\s]*$/;
    const reg1 = new RegExp(/[&]/g, "");
    const iconRule2 =
        /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/gi;
    if (!str) return callback();
    if (!reg.test(str)) return callback(new Error("暂不支持使用空格"));
    if (reg1.test(str)) return callback(new Error('暂不支持使用特殊字符"&"'));
    if (iconRule2.test(str)) return callback(new Error("暂不支持使用表情符号"));
    callback();
};

/**
 * @param {string} username
 * @returns {Boolean}
 * @lwj
 */
export const validateUsername = (rule, value, callback) => {
    if (/[^\w\.\/]/gi.test(value))
        return callback(new Error("请不要输入中文和空格！"));
    const reg = /^(?!^[0-9]*$)^([a-zA-Z0-9]{1,20}$)/;
    if (!reg.test(value))
        return callback(new Error("用户名可由英文和数字组成，不能是纯数字"));
    callback();
};

/**
 * @param {string} username  --非必填
 * @returns {Boolean}
 * @lwj
 */
export const validateUsername2 = (rule, value, callback) => {
    if (value == "") return callback();
    if (/[^\w\.\/]/gi.test(value))
        return callback(new Error("请不要输入中文和空格！"));
    const reg = /^(?!^[0-9]*$)^([a-zA-Z0-9]{1,20}$)/;
    if (!reg.test(value))
        return callback(new Error("不能是纯数字(英文+数字组成)"));
    callback();
};

export const validatename = (rule, value, callback) => {
    const reg = /[^\u4e00-\u9fa5]/g;
    if (reg.test(value)) return callback(new Error("姓名只能为中文"));
    callback();
};

export const validateNoChinese = (rule, value, callback) => {
    const reg = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
    if (reg.test(value)) return callback(new Error("不能包含中文"));
    callback();
};

export const validateNumber = (rule, value, callback) => {
    const reg = /[^\d]/g; // 正则匹配：非数字字符（等价于 /\D/g）
    if (reg.test(value)) {
        return callback(new Error("只能输入数字"));
    }
    callback();
};

export const validateNumberLetter = (rule, value, callback) => {
    const reg = /^[a-zA-Z0-9]+$/; // 正则匹配：仅允许数字和字母
    if (!reg.test(value)) {
        return callback(new Error("只能输入数字和字母")); // 错误提示
    }
    callback();
};

/**
 * @name: mobile 验证--非必填
 * @msg:
 * @param {*} rule
 * @param {*} value
 * @param {*} callback
 * @return {*}
 */
export const validMobile2 = (rule, value, callback) => {
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (value == "") return callback();
    if (!reg.test(value)) return callback(new Error("请输入正确的值班室电话"));
    callback();
};

/**
 * @name: 判断固话--非必填
 * @msg:
 * @return {*}
 */
export const validPhoneLandlineNotRequired = (rule, str, callback) => {
    const phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    const landlineReg = /\d{7,12}/;
    if (!str) return callback();
    if (!phoneReg.test(str) && !landlineReg.test(str))
        return callback(new Error("请输入正确的值班室电话"));
    callback();
};

// 座机号和手机号
export const validPhoneLandlineNotRequired2 = (rule, value, callback) => {
    // const reg = /^(0\d{2,3}-\d{7,8})|(1[3584]\d{9})$/
    // if (!reg.test(value)) return callback(new Error("请输入正确的手机号码或者座机号"))
    // callback()

    let regPone = null;
    let mobile = /^1(3|4|5|6|7|8|9)\d{9}$/; //最新16手机正则
    let tel = /^(0\d{2,3}){0,1}\d{7,8}$/; //座机
    // let tel = /^(0\d{2,3}-){0,1}\d{7,8}$/; //座机
    if (value.charAt(0) == 0) {
        // charAt查找第一个字符方法，用来判断输入的是座机还是手机号
        regPone = tel;
    } else {
        regPone = mobile;
    }
    if (!regPone.test(value)) {
        callback(new Error("请输入正确的手机号或座机号"));
    }
    callback();
};

// 身份证号--非必填
export const checkRegNo = (rule, value, callback) => {
    if (value == "") return callback();
    if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
        callback(new Error("你输入的身份证长度或格式错误"));
    }
    //身份证城市
    var aCity = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外",
    };
    if (!aCity[parseInt(value.substr(0, 2))]) {
        callback(new Error("你的身份证地区非法"));
    }
    // 出生日期验证
    var sBirthday = (
            value.substr(6, 4) +
            "-" +
            Number(value.substr(10, 2)) +
            "-" +
            Number(value.substr(12, 2))
        ).replace(/-/g, "/"),
        d = new Date(sBirthday);
    if (
        sBirthday !=
        d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()
    ) {
        callback(new Error("身份证上的出生日期非法"));
    }

    // 身份证号码校验
    var sum = 0,
        weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
        codes = "10X98765432";
    for (var i = 0; i < value.length - 1; i++) {
        sum += value[i] * weights[i];
    }
    var last = codes[sum % 11]; //计算出来的最后一位身份证号码
    if (value[value.length - 1] != last) {
        callback(new Error("你输入的身份证号非法"));
    }
    callback();
};